import redis.clients.jedis.Jedis;
import redis.clients.jedis.JedisPool;

import java.util.HashMap;
import java.util.List;
import java.util.Set;

/**
 * Created with IntelliJ IDEA.
 * Description:
 * User: lishuo
 * Date: 2023-12-17
 * Time: 9:39
 */
public class RedisHash {

    public static void test1(Jedis jedis) {
        System.out.println("hset/hget");

        jedis.flushAll();
        jedis.hset("key1","f1","111");
        HashMap<String,String> map = new HashMap<>();
        map.put("f2","222");
        map.put("f3","333");
//        jedis.hset("key1",map);

        String res1 = jedis.hget("key1", "f1");
        System.out.println("res1 = "+res1);

        String res2 = jedis.hget("key1", "f2");
        System.out.println("res2 = "+res2);

        String res3 = jedis.hget("key1","f5");
        System.out.println("res3 = "+res3);
    }

    public static void test2(Jedis jedis) {
        System.out.println("hexists/hdel");

        jedis.flushAll();
        jedis.hset("key1","f1","111");
        jedis.hset("key1","f2","111");
        jedis.hset("key1","f3","111");
        boolean hexists = jedis.hexists("key1", "f1");
        System.out.println(hexists);

        jedis.hdel("key1","f1");
        hexists = jedis.hexists("key1","f1");
        System.out.println(hexists);
    }

    public static void test3(Jedis jedis) {
        System.out.println("hkeys/hvals");

        jedis.flushAll();
        jedis.hset("key1","f1","111");
        jedis.hset("key1","f2","111");
        jedis.hset("key1","f3","111");
        jedis.hset("key1","f4","111");
        Set<String> keys = jedis.hkeys("key1");
        List<String> vals = jedis.hvals("key1");
        System.out.println("keys = "+keys);
        System.out.println("vals = "+vals);
    }

    public static void test4(Jedis jedis) {
        System.out.println("hmset/hmget");

        jedis.flushAll();
        HashMap<String,String> map = new HashMap<>();
        map.put("f1","111");
        map.put("f2","111");
        map.put("f3","111");
        map.put("f4","111");
        jedis.hmset("key1",map);

        List<String> mget = jedis.hmget("key1", "f1", "f2", "f3", "f4", "f5");
        System.out.println("mget = "+mget);

        long len = jedis.hlen("key1");
        System.out.println("len = "+len);
    }

    public static void main(String[] args) {
        JedisPool jedisPool = new JedisPool("tcp://127.0.0.1:6379");
        try (Jedis jedis = jedisPool.getResource()) {
//            test1(jedis);
//            test2(jedis);
//            test3(jedis);
            test4(jedis);
        }
    }
}
